package com.general.message.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.general.common.utils.SecurityUtils;
import com.general.message.notice.entity.NoticeInfo;
import com.general.message.notice.mapper.NoticeInfoMapper;
import com.general.message.notice.service.INoticeInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeInfoServiceImpl  extends ServiceImpl<NoticeInfoMapper, NoticeInfo> implements INoticeInfoService {

    @Autowired
    private NoticeInfoMapper noticeInfoMapper;

    @Override
    public IPage<NoticeInfo> searchNotifications() {
        return this.page(new Page<NoticeInfo>(0, 10),  new LambdaQueryWrapper<NoticeInfo>().eq(NoticeInfo::getReceiveUser, SecurityUtils.getUserId()));
    }

    /**
     * 获取个人所有消息
     * @return
     */
    @Override
    public IPage<NoticeInfo> getAllInfo(){
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return noticeInfoMapper.getAllInfo(new Page<NoticeInfo>(0, 10),userId);
    }

    /**
     * 获取单菜单用户通知列表
     * @param menuId
     * @return
     */
    public List<String> getUserList(String menuId){
        return noticeInfoMapper.getUserList(menuId);
    }


    /**
     * 获取多菜单权限用户通知列表
     * @param menuIdList
     * @return
     */
    @Override
    public List<String> getUserLists(List<String> menuIdList){
        return noticeInfoMapper.getUserLists(menuIdList);
    }

    /**
     * 未读改已读e
     *
     * @param noticeId 通知id
     */
    @Override
    public boolean readNotice(int noticeId) {
        LambdaUpdateWrapper<NoticeInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NoticeInfo::getId, noticeId)
                // 将未读的标记为已读
                .eq(NoticeInfo::getHasRead, 0)
                .eq(NoticeInfo::getId, SecurityUtils.getUserId())
                .set(NoticeInfo::getReadTime, LocalDateTime.now())
                .set(NoticeInfo::getHasRead, 1);

        return this.update(updateWrapper);
    }

    /**
     * 全部已读
     *
     * @return boolean
     */
    @Override
    public boolean readAllNotice(){
        LambdaUpdateWrapper<NoticeInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NoticeInfo::getReceiveUser, SecurityUtils.getUserId())
                // 将未读的标记为已读
                .eq(NoticeInfo::getHasRead, 0)
                .set(NoticeInfo::getReadTime, LocalDateTime.now())
                .set(NoticeInfo::getHasRead, 1);

        return this.update(updateWrapper);
    }
}
